/*
 * IdRedirector.java
 *
 * Created on April 5, 2007, 1:03 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app;

import java.sql.SQLException;
import java.util.UUID;
import java.util.logging.Level;
import org.atomojo.app.db.DB;
import org.atomojo.app.db.Feed;
import org.restlet.Application;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Reference;
import org.restlet.routing.Redirector;

/**
 *
 * @author alex
 */
public class IdRedirector extends Redirector
{
   
   Reference feedRoot;
   Application app;
   DB atomDB;
   
   /** Creates a new instance of IdRedirector */
   public IdRedirector(Context context,Application app,DB atomDB,Reference feedRoot)
   {
      super(context,null,MODE_CLIENT_FOUND);
      this.app = app;
      this.atomDB = atomDB;
      this.feedRoot = feedRoot;
   }
   
   protected Reference getTargetRef(Request request,Response response) {
      try {
         String remainingPart = request.getResourceRef().getRemainingPart();
         String [] segments = remainingPart.split("/");
         Reference ref = null;
         if (segments.length==3) {
            // The form /I/{feed-id}/_/{entry-id}
            if (!segments[1].equals("_")) {
               return null;
            }
            UUID feedId = UUID.fromString(segments[0]);
            Feed f = atomDB.findFeed(feedId);
            if (f==null) {
               return null;
            }
            UUID entryId = UUID.fromString(segments[2]);
            if (feedRoot.isAbsolute()) {
               ref = new Reference(feedRoot+f.getPath()+"_/"+entryId);
            } else {
               ref = new Reference(request.getRootRef().toString()+feedRoot+f.getPath()+"_/"+entryId);
            }
         } else if (segments.length==2) {
            // The form /I/{feed-id}/{resource}
            if (segments[1].equals("_")) {
               return null;
            }
            UUID feedId = UUID.fromString(segments[0]);
            Feed f = atomDB.findFeed(feedId);
            if (f==null) {
               return null;
            }
            if (feedRoot.isAbsolute()) {
               ref = new Reference(feedRoot+f.getPath()+segments[1]);
            } else {
               ref = new Reference(request.getRootRef().toString()+feedRoot+f.getPath()+segments[1]);
            }
         } else if (segments.length==1) {
            UUID feedId = UUID.fromString(segments[0]);
            // The form /I/{feed-id}
            Feed f = atomDB.findFeed(feedId);
            if (f==null) {
               return null;
            }
            if (feedRoot.isAbsolute()) {
               ref = new Reference(feedRoot+f.getPath());
            } else {
               ref = new Reference(request.getRootRef().toString()+feedRoot+f.getPath());
            }
         }
         getContext().getLogger().info("Redirect to: "+ref);
         return ref;
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Database access failed.",ex);
         return null;
      }
   }
   
}
